package org.example.dayu521;

import java.util.function.Consumer;

public record OrRes<T> (
    int code,
    String info,
    T res
){
    public static Integer OK=0;
    public static Integer ERROR=-1;

    public OrRes<T> info_addtion_to(Consumer<? super String> accept){
        accept.accept(info);
        return this;
    }

    public OrRes<T> throw_x() throws ORExecption {
        throw ORExecption.X(this,this.info);
    }

    public <U> OrRes<U> x(){
        return (OrRes<U>)this;
    }

    public boolean is_ok(){
        return code==0;
    }


    public static OrRes<?> error(){
        return error("");
    }

    public static OrRes<?> error(String err_msg){
        return error(ERROR,err_msg);
    }

    public static<U> OrRes<U> error_res(U res){
        return new OrRes(-1,"",res);
    }

    public static OrRes<?> error(int code,String err_msg){
        return new OrRes<>(code,err_msg,"");
    }

    public static <U> OrRes<U> ok(U res){
        return new OrRes<>(0,"ok",res);
    }

    public static OrRes<?> ok(){
        return OrRes.ok("");
    }

    public static OrRes<?> x_error(String msg) throws ORExecption {
        return error(msg).throw_x();
    }
}
